import requests
import json
from app.web.comm.operate_json import  OperationJson
# from config.config import  basedir
from app.web.conf.config import  *
class OperationHeader(OperationJson):
    def __init__(self,response=None):
    # def __init__(self):
        self.opera=OperationJson()
        self.response=response

    #根据关键字获取数据
    def get_data(self, file_path, id):
        return self.read_data(file_path)[id]

    def write_cookie(self):#最终调用写入cookie的方法

        cook = self.response.cookies
        cooki = json.dumps(requests.utils.dict_from_cookiejar(cook),ensure_ascii=False,indent=4)#dict转成str
        print(type(cooki))
        cookie = self.opera.write_cookie_data(cooki.encode('utf-8'))
        return cookie

    def write_token(self):#写入token的最终调用方法
        toke = json.dumps(self.response['data'])#dict转成str
        token = self.opera.write_token_data(toke.encode('utf-8'))
        return token

#读取cookie数据
    def get_cookie(self):
        cookies = self.read_data(file_path=basedir +'\\data\\cookie.json')
        return cookies

#读取token数据
    def get_token(self):
        token = self.get_data(file_path=basedir +'\\data\\token.json',id='token')
        return token
#调试
# if __name__=="__main__":

